package com.hyf.dagger2.app;

import android.app.Activity;
import android.app.Application;

import com.hyf.dagger2.di.component.AppComponent;
import com.hyf.dagger2.di.component.DaggerAppComponent;
import com.hyf.dagger2.di.module.AppModule;

import javax.inject.Inject;

import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasActivityInjector;

/**
 * Created by HyFun on 2018/11/20.
 * Email: 775183940@qq.com
 * Description:
 */

public class Dagger2Application extends Application implements HasActivityInjector {

    private static volatile AppComponent appComponent;
    private static Dagger2Application instance;

    @Inject
    DispatchingAndroidInjector<Activity> mAndroidInjector;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        DaggerAppComponent.builder()
                .appModule(new AppModule(instance))
                .build().inject(instance);
    }


    public static synchronized AppComponent getAppComponent() {
        if (appComponent == null) {
            appComponent = DaggerAppComponent.builder()
                    .appModule(new AppModule(instance))
                    .build();
        }
        return appComponent;
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return mAndroidInjector;
    }
}
